package com.globaldom.web.enemy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.PersonLocation;
import com.globaldom.utils.Constants;
import com.globaldom.utils.MemCacheManager;

@Controller
@RequestMapping(value = "/getallcombatant")
public class CombatantGetAllController {

	private static final Logger log = Logger.getLogger(CombatantGetAllController.class.getName());

	public CombatantGetAllController() {
	}

	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> getCombatants(@RequestParam float nlat, @RequestParam float nlon, @RequestParam float slat,
			@RequestParam float slon, @RequestParam int hc, HttpSession session,HttpServletRequest request) {

		return forcegetCombatants(nlat, nlon, slat, slon, hc, true, session,request);
	}

	@RequestMapping(value = "/forceget", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> forcegetCombatants(@RequestParam float nlat, @RequestParam float nlon,
			@RequestParam float slat, @RequestParam float slon, @RequestParam int hc, boolean force, HttpSession session,HttpServletRequest request) {
		Map<String, String> map = null;
		map = new HashMap<String, String>();
		String personID = (String) session.getAttribute("personID");
		if (null == personID) {

			
			Cookie[] cookies = request.getCookies();
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				if ((cookie.getName().equals("a"))) {
					String id = cookie.getValue();
					Map<String, Object> m = MemCacheManager.getAuthStats(id);
					if (null != m)
					{
						personID = (String) m.get("personID");
						session.setAttribute("personID", personID);
					}
					else
					{
						map.put(Constants.STATUS, "-ER");
						map.put(Constants.CODE, "401");
						return map;
					}
				}

			}
			
			
		
		}
//		if (!force) {
//			map = MemCacheManager.getCombatants(personID);
//			if (null != map)
//				return map;
//		}
		
		// EntityManager em = EMF.get().createEntityManager();
		try {
			// FIXME the home country variable hc should either be retrieved
			// from session or DB.
			//log.log(Level.WARNING, "About to locate locations...");
			List<PersonLocation> list = new ArrayList<PersonLocation>();//PersonLocation.findCombatantsbyBounds(personID, nlat, nlon, slat, slon);
			//log.log(Level.WARNING, "returned from locations " + list);
			for (Iterator<PersonLocation> iterator = list.iterator(); iterator.hasNext();) {
				PersonLocation personLocation = (PersonLocation) iterator.next();
				if (! personLocation.getId().equals((personID))) {
					map.put(personLocation.getLatitude() + "/" + personLocation.getLongitude(), personLocation.getId()
							 + "/" + personLocation.getPerson());
				}
			}
		} catch (Exception ex) {
			log.log(Level.SEVERE, ex + " msg:" + ex.getMessage());

			ex.printStackTrace();
		}
//		MemCacheManager.putCombatants(personID, map);
		return map;
	}

	// initializing method
	@RequestMapping(method = RequestMethod.GET)
	public String getCreateForm(Model model, HttpSession session) {
		return "redirect:/country";
	}

}
